//
//  AFNetworking.m
//  ProjectB_Only.Car
//
//  Created by 李雪 on 16/1/20.
//  Copyright © 2016年 「ゆき」. All rights reserved.
//

#import "AFNetworking.h"
#import <AFNetworking/AFNetworking.h>
#import "NewNestHeader.h"

@implementation AFNetworking
+ (void)GETWithURL:(NSString *)url completion:(NETBLOCK)netBlock
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html", nil];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager GET:url parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"Request Successed!");
        id result = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        netBlock(result);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"Request Failed");
    }];
}

+ (void)POSTWithURL:(NSString *)url body:(NSString *)body completion:(NETBLOCK)netBlock
{
    NSDictionary *bodyDic = [[self class] stringChangedDic:body];
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html", nil];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager POST:url parameters:bodyDic progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"Request Successed!");
        id result = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        netBlock(result);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"Request Failed");
    }];
}
// 拆分 字符串->字典
+ (NSDictionary *)stringChangedDic:(NSString *)body
{
    NSMutableDictionary *mDic = [NSMutableDictionary dictionary];
    NSArray *arr = [body componentsSeparatedByString:@"&"];
    NSMutableArray *tempArr = [NSMutableArray array];
    for (NSString *str in arr) {
        tempArr = [[str componentsSeparatedByString:@"="] mutableCopy];
        [mDic setObject:tempArr.lastObject forKey:tempArr.firstObject];
        [tempArr removeAllObjects];
    }
//    NSDictionary *dic = mDic;
    [mDic removeObjectForKey:@""];
    return mDic;
}

#pragma mark - 网络请求状态判断
+ (void)networkReachabilityStatus:(NETBLOCK)block
{
    // 网络连接检测封装
    AFNetworkReachabilityManager *reachManeger = [AFNetworkReachabilityManager sharedManager];
    // 开始检测
    [reachManeger startMonitoring];
    
    [reachManeger setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        switch (status) {
            case AFNetworkReachabilityStatusNotReachable:
                block(@0);   //没网状态
                break;
            case AFNetworkReachabilityStatusReachableViaWWAN:
                block(@1);   //流量
                break;
            case AFNetworkReachabilityStatusReachableViaWiFi:
                block(@2);    //wifi
                break;
            case AFNetworkReachabilityStatusUnknown:
                block(@(-1));
                break;    //未知网络状态
            default:
                break;
        }
    }];
    
}




@end
